
/**
 * Sheet 1, exercise 1
 * A class that calculate the gdc by using the given methods.
 * 
 * @author Dennis Meyer, Sebastian Brodehl, Sebastian Schappert
 * @version 1.0.0.0
 *
 */

public class Euklid 
{	
	public static int rounds;
	
	public static void main(String[] argv)
	{
		System.out.println(Euklid.calcGdc(126, 8, true));
		System.out.println(Euklid.rounds);
	}
	
	/**
	 * Calculate the gdc.
	 * 
	 * @param bigger first number
	 * @param smaller second number
	 * @param minusMethod true if method to calculate is minus. false if method is to be modulo.
	 * @return the gdc.
	 */
	public static int calcGdc(int bigger, int smaller, boolean minusMethod)
	{
		try 
		{
			// Throw exception if bigger or smaller are negative
			if (bigger < 0 || smaller < 0)
				throw new Exception("Both numbers must be positive!");
			if (bigger == smaller)
				throw new Exception("The numbers are equeal");
		}
		catch (Exception ex)
		{
			System.out.println("Error: " + ex.getMessage());
			return 0;
		}
		
		// switch Numbers if number2 is bigger than number1
		if (smaller > bigger)
		{
			int temp = bigger;
			bigger = smaller;
			smaller = temp;
		}
		
		Euklid.rounds = 0;
		return minusMethod ? minus(bigger, smaller) : modulo(bigger, smaller);	
		
	}
	
	private static int minus(int bigger, int smaller)
	{
		rounds++;
		bigger -= smaller;
		if (bigger > 0 && smaller > 0)
			bigger = minus(bigger > smaller ? bigger : smaller, bigger > smaller ? smaller : bigger);
		
		return bigger == 0 ? smaller : bigger;

	}
	
	private static int modulo(int bigger, int smaller)
	{
		rounds++;
		bigger %= smaller;
		if (bigger > 0 && smaller > 0)
			bigger = modulo(bigger > smaller ? bigger : smaller, bigger > smaller ? smaller : bigger);
		
		return bigger == 0 ? smaller : bigger;
	}
}
